package fw_ws

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/olahol/melody"
)

var m *melody.Melody

func WsInit(r *gin.Engine) {
	m = melody.New()
	r.GET("/ws/v1/", func(c *gin.Context) {
		m.HandleRequest(c.Writer, c.Request)
	})
	m.HandleConnect(func(s *melody.Session) {
		fmt.Println("连接成功----->", s.LocalAddr())
	})
	m.HandleDisconnect(func(s *melody.Session) {
		fmt.Println("连接断开----->", s.LocalAddr())
	})
	m.HandleMessage(func(s *melody.Session, msg []byte) {
		//sessions, _ := m.Sessions()
		//for _,v := range sessions {
		//	v.
		//}
		//sendBroadCast([]byte("asfasdfsdfsdf"))
		//fmt.Println("收到信息----->", s.LocalAddr(), string(msg), sessions)
	})
}

func SendBroadCast(msg []byte) {
	m.Broadcast(msg)
}
